<template>
  <div class="content">
    <div class="header">
      <h3 class="title">{{ contentConfig?.header?.title ?? '数据列表' }}</h3>
      <el-button type="primary" @click="handleNewUserClick">{{
        contentConfig?.header?.btnTitle ?? '新建数据'
      }}</el-button>
    </div>
    <div class="table">
      <el-table :data="pageList" style="width: 100%" v-bind="contentConfig?.childrenTree">
        <template v-for="item in contentConfig?.propsList" :key="item.prop">
          <!-- v-bind="item" 跟 name="name" :age="age"一样  v-bind直接绑定对象 -->
          <template v-if="item.type === 'timer'">
            <el-table-column v-bind="item">
              <template #default="scope"> {{ formatUTC(scope.row[item.prop]) }} </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type === 'handler'">
            <el-table-column v-bind="item">
              <template #default="scope">
                <el-button size="small" type="primary" text @click="handleEditBtnClick(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="small"
                  type="danger"
                  text
                  @click="handleDeleteBtnClick(scope.row.id)"
                  >删除</el-button
                ></template
              >
            </el-table-column>
          </template>
          <template v-else-if="item.type === 'custom'">
            <el-table-column v-bind="item">
              <template #default="scope">
                <slot :name="item.slotName" v-bind="scope" :prop="item.prop"></slot>
              </template>
            </el-table-column>
          </template>
          <template v-else>
            <el-table-column v-bind="item" align="center"> </el-table-column
          ></template>
        </template>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { formatUTC } from '@/utils/format'
import { storeToRefs } from 'pinia'
import useSystemStore from '@/store/main/system/system'

// 接收父组件传来的值
interface IProps {
  contentConfig: {
    pageName: string
    header?: {
      title?: string
      btnTitle?: string
    }
    propsList: any[]
    childrenTree?: any
  }
}
const props = defineProps<IProps>()

// 定义事件用来抛发弹窗事件
const emit = defineEmits(['newBtnClick', 'editBtnClick'])

// 1. 发起action,请求列表数据
const currentPage = ref(1)
const pageSize = ref(10)
const systemStore = useSystemStore()
fetchPageListData()

// 2. 获取数据
// storeToRefs 是一个响应式的
const { pageList, pageTotalCount } = storeToRefs(systemStore)

// 3. 页码校验

function handleSizeChange() {
  fetchPageListData()
}
function handleCurrentChange() {
  fetchPageListData()
}

// 4. 定义函数,用于发送网络请求
function fetchPageListData(formData: any = {}) {
  // 获取分页
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset } // 页码

  // 发送网络请求
  const queryInfo = { ...pageInfo, ...formData } // 搜索框
  systemStore.postPageListAction(props.contentConfig.pageName, queryInfo)
}

// 5. 删除功能
function handleDeleteBtnClick(id: number) {
  systemStore.deletePageByIdAction(props.contentConfig.pageName, id)
}

// 6. 新建用户功能
function handleNewUserClick() {
  emit('newBtnClick')
}
function handleEditBtnClick(itemData: any) {
  emit('editBtnClick', itemData)
}

// 暴露网络请求的方式,这样就可以通过ref访问了
defineExpose({
  fetchPageListData
})
</script>

<style scoped lang="less">
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}
.header {
  display: flex;
  justify-content: space-between;
  h3 {
    margin: 0;
    padding: 0;
    font-size: 22px;
  }
  .title {
    // font-size: 30px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
